

import { defHttp } from "/@/utils/http/axios/nss";


enum Api {
  // /flow/node/list
  list = '/system/flow/flowRecord/list',// 流程记录分页列表查询 POST
  save = '/system/flow/flowRecode/saveOrUpdate',//  流程记录新增/修改  POST
  delete = '/system/flow/flowRecord / remove ', //  流程记录删除 POST 
  edit = '/pms/lease/customer/update',
  get = '/system/flow/flowRecord/detail',  // POST  流程记录查看详情 根据id查询
  getFlowNodes = '/system/flow/node/node',// { flowId } POST 获取工作流节点

}
import { useUserStore } from "/@/store/modules/user";
import { useWorkParamsStore } from "/@/store/modules/workParams";
const store = useUserStore()
const storeNum = useWorkParamsStore()
/**
 * 查询列表
 * @param params
 */
export const getFlowRecordList = async (params) => {
  console.log('params', params);

  const data = {
    query: [
      {
        recordState: params.recordState ? params.recordState : '',
      },
      // {
      //   columnName: "keyWords",
      //   value: params.keyWords ? params.keyWords : {},
      //   whereType: "eq"
      // },
      // {
      //   columnName: "handleUser",
      //   value: store.userInfo.userId ? store.userInfo.userId : '',
      //   whereType: "eq"
      // }
    ],
    page: {
      current: params.pageNo,
      size: params.pageSize
    }
  }
  const useId = store.userInfo.userId
  const { records } = await defHttp.post({ url: Api.list, data });
  // 过滤条件：进行中的审批工作，处理人中包含当前登录人（userId）
  const filterRecords = records.filter(item => {
    // 处理人字符串转数组
    let arr1 = item.handleUser.split(',')
    return arr1.includes(useId + '') && item.recordState === "2"
  })

  return filterRecords;
};

/**
 * 新增/修改流程记录
 * @param params
 */
export const saveOrUpdateFlowRecord = (params) => {
  return defHttp.post({ url: Api.save, params });
};

/**
 * 删除流程记录
 * @param id 记录主键ID
 * @param handleSuccess 删除成功后的回调（可选）
 */
export const deleteFlowRecordById = (id, handleSuccess) => {
  const url = Api.delete.replace(/\s+/g, ''); // 移除空格
  return defHttp.post({ url: `${url}/${id}` }, { joinParamsToUrl: true }).then(() => {
    if (handleSuccess instanceof Function) {
      handleSuccess();
    }
  });
};

/**
 * 获取指定 ID 的流程记录详情
 * @param id 记录主键ID
 */
export const getFlowRecordDetailById = (id) => {
  return defHttp.post({ url: Api.get, params: { id } });
};


/**
 * 获取指定 ID 的流程节点信息
 * @param flowId 
 */
export const getFlowNodes = (id) => {
  return defHttp.post({ url: `${Api.getFlowNodes}/${id}` });
};